package com.ray.auth.common;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.service.AppService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.service.NoticeService;
import com.ray.auth.system.service.OrganizationService;
import com.ray.auth.system.service.UserService;
import com.ray.auth.system.service.WorkService;
import com.ray.auth.system.table.entity.App;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.Notice;
import com.ray.auth.system.table.entity.Organization;
import com.ray.auth.system.table.entity.User;
import com.ray.auth.system.table.entity.Work;
import com.ray.common.util.DictLoadUtil;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.fastdfs.spring.FastDfsFactory;
import com.zjhc.flyhong.fastdfs.utils.UploadInfo;
import com.zjhc.web.controller.WebController;
import com.zjhc.web.util.ContextHolderUtils;

/**
 * <h1>通用树 控制器</h1>
 * 
 * @创建人 ray
 * @创建时间 2017-06-12
 * @版本 1.0
 * @重要修改备注
 * 
 */
@Controller
@RequestMapping("/common")
public class CommonController extends WebController{
	@Autowired
	private UserService userService;
	@Autowired
	private AppService appService;
	@Autowired
	private DictService dictService;
	@Autowired
	private WorkService workService;
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private FastDfsFactory fastDfsFactory; 
	@Autowired 
	private DictLoadUtil DictLoadUtil;


	private static Logger logger = Logger.getLogger(CommonController.class);
	/**
	 * 个人中心
	 * @return
	 */
	@RequestMapping(value = "profile")
	public ModelAndView profile(String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser Loginuser = getLoginUser();
	  User user = userService.queryUser(Loginuser.getUserId(), Loginuser);
	  
     Organization org = organizationService.queryOrg(user.getOrgCode(), Loginuser);
	  
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"userType","sex","useStatus");
	 
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("orgName", org.getOrgName());
	  modelAndView.addObject("record", user);
	  modelAndView.setViewName("common/profile/profile");
	  return modelAndView;
	}
	
	/**
	 * 修改密码
	 * @return
	 */
	@RequestMapping(value = "password")
	public ModelAndView password(String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  modelAndView.addObject("record", user.getExtendUser());
	  modelAndView.setViewName("common/password/password");
	  return modelAndView;
	}

	/**
	 * 更新密码
	 * @return
	 */
	@RequestMapping(value = "updatePassword")
	@ResponseBody
	public ResponseEntity<Result<Object>> updatePassword(User params,String hisPassword, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.updatePassword(params,hisPassword,loginUser);
		return doResult(result);
	}
	/**
	 * 更新用户信息
	 * @return
	 */
	@RequestMapping(value = "updateProfile")
	@ResponseBody
	public ResponseEntity<Result<Object>> updateProfile(User params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.updateUser(params, loginUser);
		if(result.codeSuccess()){
			//更新session 用户信息
			User user = userService.queryUser(params.getUserId(), loginUser);
			loginUser.setExtendUser(user);
			ContextHolderUtils.setLoginUser(loginUser);
		}
		return doResult(result);
	}
	
	/**
	 * 上传图片
	 * @return
	 */
	@RequestMapping(value = "uploadUserImage")
	@ResponseBody
	public ResponseEntity<Result<Object>> uploadUserImage(User params, HttpServletRequest request, HttpServletResponse respose){
		Result<Object> result = null;
		try {
			LoginUser loginUser = getLoginUser();
			MultipartFile file = getMultipartFile(request, "file");
			if(ObjectUtil.isNotNull(file)){
				UploadInfo info = fastDfsFactory.upload(file.getBytes(), FileUtil.extName(file.getOriginalFilename()));
				//获取图片上传地址
				String photoUrl = info.getUrl();
				params.setPhotoUrl(photoUrl);
			}
			result = userService.updateUser(params, loginUser);
		} catch (Exception e) {
			logger.error("上传失败",e);
			result = new Result<Object>(-1, "上传失败");
		} 
		return doResult(result);
	}
	
	/**
	 * 我的页面
	 * @return
	 */
	@RequestMapping(value = "my")
	public ModelAndView my(String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  List<App> apps = appService.queryUseAppList();
	  modelAndView.addObject("apps", apps);
	  modelAndView.addObject("record", user.getExtendUser());
	  modelAndView.setViewName("my");
	  return modelAndView;
	}
	
	
	/**
	 * 工作详情
	 * @return
	 */
	@RequestMapping(value = "workDetail")
	public ModelAndView workDetail(String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Work record =  workService.queryWork(id, user);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("common/work/detail");
	  return modelAndView;
	}
	
	/**
	 * 通知详情
	 * @return
	 */
	@RequestMapping(value = "noticeDetail")
	public ModelAndView noticeDetail(String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Notice record =  noticeService.queryNotice(id, user);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("common/notice/detail");
	  return modelAndView;
	}
	
	/**
	 * 通知详情
	 * @return
	 */
	@RequestMapping(value = "noticeDetailWindow")
	public ModelAndView noticeDetailWindow(String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Notice record =  noticeService.queryNotice(id, user);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("common/notice/window");
	  return modelAndView;
	}
	
	/**
	 * 最新通知
	 * @return
	 */
	@RequestMapping(value = "windowNotice")
	@ResponseBody
	public ResponseEntity<Result<Object>> windowNotice(HttpServletRequest request, HttpServletResponse respose){
		Result<Object> result = null;
		LoginUser user = getLoginUser();
		Notice notice = noticeService.queryFirstWindowNotice(user);
		if(notice == null){
			result = new Result<Object>(-1, "无弹窗通知");
		}else{
			result = new Result<Object>(0, "",notice);
		}
		return doResult(result);
	}
	
}
